//
//  DetailViewController.m
//  JWMovie
//
//  Created by Jorn.Wu on 15/7/20.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "DetailViewController.h"
#import "JWDetailTopView.h"
#import "JWMovieDetailModel.h"
#import "JWDetailScrollViewCell.h"
#import "UIImageView+WebCache.h"
#import "JWMovieDetailCommentModel.h"
#import "JWMovieDetailCommentCell.h"

#import "constant.h"

@interface DetailViewController ()<UITableViewDataSource,UITableViewDelegate,JWMovieDetailCommentModelDelegate>
@property (nonatomic, strong) JWMovieDetailModel *movieDetailModel;
@property (nonatomic, strong) JWDetailTopView *topView;
@property (nonatomic, strong) UIScrollView *mScrollView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *commentAr;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, assign) BOOL open;

@end

@implementation DetailViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"电影详情";
        self.navigationController.navigationBar.translucent = NO;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    

    [self ceateTableViewHeader];
    [self addTopView];
    [self addScrollView];
    [self addTableView];
    
}

//------------------------------------------头部视图--------------------------------------------------------------------

- (JWMovieDetailModel *)movieDetailModel{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie_detail" ofType:@"json"];
    NSData *jsonData = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:nil];
    NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    
    if (!_movieDetailModel) {
        _movieDetailModel = [JWMovieDetailModel movieDetailModelWithDictionary:dic];
    }
    
    return _movieDetailModel;
}

- (void)addTopView{
    
    _topView = [[JWDetailTopView alloc] init];
    _topView.movieDetailModel = self.movieDetailModel;
    _topView.view.frame = CGRectMake(5, 0, self.view.frame.size.width - 10, 155);
    
    [self.headerView addSubview:_topView.view];
}

#pragma mark - 添加滚动视图

- (void)addScrollView{
    
    NSArray *imageUrlAr = self.movieDetailModel.images;
    
    _mScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 150, JWScreenWidth - 20, 100)];
    _mScrollView.contentSize = CGSizeMake(100 * imageUrlAr.count , 100);
    _mScrollView.backgroundColor = [UIColor clearColor];
    _mScrollView.layer.cornerRadius = 8.f;
    _mScrollView.layer.borderColor = [UIColor whiteColor].CGColor;
    _mScrollView.layer.borderWidth = 2;
    
    
    [self.headerView addSubview:_mScrollView];
    
    for (int i = 0; i < imageUrlAr.count; i++) {
        
        JWDetailScrollViewCell *cellScrollView = [[JWDetailScrollViewCell alloc] initWithFrame:CGRectMake((90 + 5) * i + 5, (_mScrollView.frame.size.height - 90) * 0.5, 90, 90)];
        cellScrollView.contentSize = CGSizeMake(90, 90);
        
        [cellScrollView setImageWithUrl:imageUrlAr[i]];
        
        cellScrollView.layer.cornerRadius = 0.5;
        
        [_mScrollView addSubview:cellScrollView];
     
    }
}

//------------------------------------------评论列表--------------------------------------------------------------------

#pragma mark - 添加表视图

- (void)addTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, JWScreenWidth, JWScreenHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    [_tableView setTableHeaderView:self.headerView];
    
    [self.view addSubview:_tableView];
    
  
}

- (NSArray *)commentAr{
    
    if (!_commentAr) {
        
        NSMutableDictionary *jsonDic = [self objectWithJsonName:@"movie_comment"];
        
        NSArray *listAr = [jsonDic objectForKey:@"list"];
        
        NSMutableArray *tempAr = [NSMutableArray array];
        for (int i = 0; i < listAr.count; i++) {
            NSDictionary *commentInfoDic = listAr[i];
            JWMovieDetailCommentModel *model = [JWMovieDetailCommentModel movieDetailCommentModelWithDictionary:commentInfoDic];
//            model.delegate = self;
            [tempAr addObject:model];
        }
        _commentAr = tempAr;
    }
    
    return _commentAr;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.commentAr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JWMovieDetailCommentCell *cell = [JWMovieDetailCommentCell movieDetailCommentModelWithTableView:tableView];
    JWMovieDetailCommentModel *model = self.commentAr[indexPath.row];
    
    cell.movieDetailCommentModel = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    JWMovieDetailCommentModel *Model = (JWMovieDetailCommentModel *)self.commentAr[indexPath.row];
//    NSLog(@"%lf",Model.cellHeight);
    return Model.cellHeight;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JWMovieDetailCommentModel *model = (JWMovieDetailCommentModel *)self.commentAr[indexPath.row];
    model.delegate = self;
    model.open = !model.open;
}

- (void)openTheCell{
    [self.tableView reloadData];
}


#pragma mark - 添加表头视图

- (UIView *)ceateTableViewHeader{
    _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JWScreenWidth, 250)];
    return _headerView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
